/**
 * Copyright(C) 2008 Matthias Braunhofer
 * All rights reserved
 *
 * Created on: 28.07.2008
*/

package it.cums.wizards;

import it.cums.controller.ApplicationController;
import it.cums.model.Country;

import java.util.List;

import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class CustomerBusinessWizardPage extends WizardPage implements IWizardPage {
	private Text deliveryCapText;
	private Text deliveryLocationText;
	private Text deliveryAddressText;
	private Combo deliveryCountryCombo;
	private Composite container;
	
	public CustomerBusinessWizardPage() {
		super("New Customer");
		setTitle("Delivery Information");
		setDescription("Specify delivery address (when different to customer address)");
	}

	@Override
	public void createControl(Composite parent) {
		container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 2;
		Label label1 = new Label(container, SWT.NULL);
		label1.setText("Delivery address:");
		
		deliveryAddressText = new Text(container, SWT.BORDER | SWT.SINGLE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		deliveryAddressText.setLayoutData(gd);
		deliveryAddressText.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent arg0) {
				dialogChanged();
			}
        });
		
		Label label2 = new Label(container, SWT.NULL);
		label2.setText("CAP:");
		
		deliveryCapText = new Text(container, SWT.BORDER | SWT.SINGLE);
		deliveryCapText.setLayoutData(gd);
		deliveryCapText.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent arg0) {
				dialogChanged();
			}
        });
		
		Label label3 = new Label(container, SWT.NULL);
		label3.setText("Location:");
		
		deliveryLocationText = new Text(container, SWT.BORDER | SWT.SINGLE);
		deliveryLocationText.setLayoutData(gd);
		deliveryLocationText.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent arg0) {
				dialogChanged();
			}
        });
		
		Label label4 = new Label(container, SWT.NULL);
		label4.setText("Country:");
		
		deliveryCountryCombo = new Combo(container, SWT.READ_ONLY);
		deliveryCountryCombo.setLayoutData(gd);
		initCountries();
		deliveryCountryCombo.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent arg0) {
				dialogChanged();
			}
		});
		
		setControl(container);
		setPageComplete(true);
	}
	
	private void dialogChanged() {
		if (getDeliveryAddress().length() > 0 && getDeliveryCAP().length() > 0 &&
			getDeliveryCountry().length() > 0 && getDeliveryLocation().length() > 0) {
			setPageComplete(true);
			setErrorMessage(null);
		} else if (getDeliveryAddress().length() == 0 && getDeliveryCAP().length() == 0 &&
			getDeliveryCountry().length() == 0 && getDeliveryLocation().length() == 0) {
			setPageComplete(true);
			setErrorMessage(null);
		} else {
			setErrorMessage("Either fill out all fields or let them blank");
			setPageComplete(false);
		}
	}
	
	public String getDeliveryCAP() {
		return deliveryCapText.getText().trim();
	}
	
	public String getDeliveryLocation() {
		return deliveryLocationText.getText().trim();
	}
	
	public String getDeliveryAddress() {
		return deliveryAddressText.getText().trim();
	}
	
	public String getDeliveryCountry() {
		return deliveryCountryCombo.getText().trim();
	}
	
	@Override
	public Control getControl() {
		return container;
	}
	
	private void initCountries() {
		List<Country> countries = ApplicationController.getInstance().getCountries();
		for (Country country : countries) {
			deliveryCountryCombo.add(country.getDescription());
		}
		deliveryCountryCombo.add("", 0);
	}
}
